package com.example.chinesechess.presentation.ui.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.chinesechess.R
import com.example.chinesechess.presentation.ui.Screen

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(navController: NavController) {
    // 记录是否显示AI难度选择菜单
    val showAiDifficultyMenu = remember { mutableStateOf(false) }
    
    Scaffold(
        topBar = {
            CenterAlignedTopAppBar(
                title = {
                    Text(
                        text = stringResource(R.string.app_name),
                        style = MaterialTheme.typography.titleLarge
                    )
                }
            )
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding)
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // Game logo or icon (placeholder)
            /* 
            Image(
                painter = painterResource(id = R.drawable.chess_logo),
                contentDescription = "Chinese Chess Logo",
                modifier = Modifier
                    .size(180.dp)
                    .padding(bottom = 32.dp)
            )
            */
            
            Text(
                text = "中国象棋",
                style = MaterialTheme.typography.titleLarge,
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(bottom = 32.dp)
            )

            // 如果不显示AI难度选择，则显示主菜单
            if (!showAiDifficultyMenu.value) {
                // 主菜单按钮
                MenuButton(
                    text = stringResource(R.string.menu_local_game),
                    onClick = { navController.navigate(Screen.Game.route + "/local") }
                )
                
                MenuButton(
                    text = stringResource(R.string.menu_ai_game),
                    onClick = { showAiDifficultyMenu.value = true }
                )
                
                MenuButton(
                    text = stringResource(R.string.menu_online_game),
                    onClick = { navController.navigate(Screen.Online.route) }
                )
                
                MenuButton(
                    text = stringResource(R.string.menu_game_records),
                    onClick = { navController.navigate(Screen.GameRecords.route) }
                )
                
                MenuButton(
                    text = stringResource(R.string.menu_settings),
                    onClick = { navController.navigate(Screen.Settings.route) }
                )
            } else {
                // AI难度选择菜单
                Text(
                    text = "选择AI难度",
                    style = MaterialTheme.typography.titleMedium,
                    modifier = Modifier.padding(bottom = 16.dp)
                )
                
                MenuButton(
                    text = stringResource(R.string.mode_ai_easy),
                    onClick = { 
                        navController.navigate(Screen.Game.route + "/ai_easy")
                        showAiDifficultyMenu.value = false
                    }
                )
                
                MenuButton(
                    text = stringResource(R.string.mode_ai_medium),
                    onClick = { 
                        navController.navigate(Screen.Game.route + "/ai_medium")
                        showAiDifficultyMenu.value = false
                    }
                )
                
                MenuButton(
                    text = stringResource(R.string.mode_ai_hard),
                    onClick = { 
                        navController.navigate(Screen.Game.route + "/ai_hard")
                        showAiDifficultyMenu.value = false
                    }
                )
                
                // 返回按钮
                OutlinedButton(
                    onClick = { showAiDifficultyMenu.value = false },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 8.dp)
                        .height(56.dp)
                ) {
                    Text(
                        text = "返回",
                        style = MaterialTheme.typography.titleMedium
                    )
                }
            }
        }
    }
}

@Composable
fun MenuButton(text: String, onClick: () -> Unit) {
    Button(
        onClick = onClick,
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp)
            .height(56.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.primary
        )
    ) {
        Text(
            text = text,
            style = MaterialTheme.typography.titleMedium
        )
    }
} 